package tech.abing.spider.task.pageprocessor.cfa;

import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.regex.Pattern;

import org.apache.log4j.Logger;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import tech.abing.spider.component.PageProcessor;
import tech.abing.spider.component.PageScript;
import tech.abing.spider.model.Proccessable;
import tech.abing.spider.page.OkPage;
import tech.abing.spider.page.Page;
import tech.abing.spider.request.BasicRequest;
import tech.abing.spider.request.StartContext;
import tech.abing.spider.task.model.cfa.FuturesComEntity;
import tech.abing.spider.task.pageprocessor.amac.ManagerDetailProcessor;
import tech.abing.spider.util.StringUtils;
import tech.abing.spider.util.UuIdPublic;

public class FuturesComProcessor implements PageProcessor {
	
	@Override
	public PageScript getJavaScript() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Pattern getNormalContain() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void process(OkPage page, StartContext context,
			List<BasicRequest> queue, List<Proccessable> objectContainer)
			throws Exception {
		Document doc = Jsoup.parse(page.getContent());
		String url = page.getRequest().getUrl();
//		Elements selects = doc.select("body > div.main > div.cen_main > div.news_main > div > div.news_right > div.xl_zhengwen > div.TRS_Editor " +
//				" > table ");
//		Element table = selects.first();
//		Elements trs = table.select("tr:gt(0)");
		Elements trs = doc.select("tbody").first().select("tr");
		for (Element tr : trs) {
			Elements tds = tr.select("td");
			if(tds.size()>1){
				String orgType = tds.get(1).text();
				if("期货公司".equals(orgType)){
					FuturesComEntity fce = new FuturesComEntity();
					fce.setOrgName(tds.get(0).text());
					fce.setOrgType(tds.get(1).text());
					fce.setUrl(url);
					fce.setId(UuIdPublic.timeStrongID(5));
					StringUtils.removeSpace(fce);
					objectContainer.add(fce);
				}	
			}


		}

	}

	@Override
	public void processErrorPage(Page page, StartContext context,
			List<Proccessable> objectContainer) throws Exception {
		// TODO Auto-generated method stub

	}

}
